package cn.lngaosu.lwyd.module.integration.controller.admin.weixinuser;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.CommonResult;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;
import static cn.lngaosu.lwyd.framework.common.pojo.CommonResult.success;

import cn.lngaosu.lwyd.framework.excel.core.util.ExcelUtils;

import cn.lngaosu.lwyd.framework.apilog.core.annotation.ApiAccessLog;
import static cn.lngaosu.lwyd.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.lngaosu.lwyd.module.integration.controller.admin.weixinuser.vo.*;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.weixinuser.WeixinUserDO;
import cn.lngaosu.lwyd.module.integration.service.weixinuser.WeixinUserService;

@Tag(name = "管理后台 - 微信公众号用户基本信息")
@RestController
@RequestMapping("/integration/weixin-user")
@Validated
public class WeixinUserController {

    @Resource
    private WeixinUserService weixinUserService;

    @PostMapping("/create")
    @Operation(summary = "创建微信公众号用户基本信息")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user:create')")
    public CommonResult<Long> createWeixinUser(@Valid @RequestBody WeixinUserSaveReqVO createReqVO) {
        return success(weixinUserService.createWeixinUser(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新微信公众号用户基本信息")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user:update')")
    public CommonResult<Boolean> updateWeixinUser(@Valid @RequestBody WeixinUserSaveReqVO updateReqVO) {
        weixinUserService.updateWeixinUser(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除微信公众号用户基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('integration:weixin-user:delete')")
    public CommonResult<Boolean> deleteWeixinUser(@RequestParam("id") Long id) {
        weixinUserService.deleteWeixinUser(id);
        return success(true);
    }

    @GetMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除微信公众号用户基本信息")
                @PreAuthorize("@ss.hasPermission('integration:weixin-user:delete')")
    public CommonResult<Boolean> deleteWeixinUserList(@RequestParam("ids") List<Long> ids) {
        weixinUserService.deleteWeixinUserListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信公众号用户基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user:query')")
    public CommonResult<WeixinUserRespVO> getWeixinUser(@RequestParam("id") Long id) {
        WeixinUserDO weixinUser = weixinUserService.getWeixinUser(id);
        return success(BeanUtils.toBean(weixinUser, WeixinUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信公众号用户基本信息分页")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user:query')")
    public CommonResult<PageResult<WeixinUserRespVO>> getWeixinUserPage(@Valid WeixinUserPageReqVO pageReqVO) {
        PageResult<WeixinUserDO> pageResult = weixinUserService.getWeixinUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WeixinUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信公众号用户基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('integration:weixin-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWeixinUserExcel(@Valid WeixinUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeixinUserDO> list = weixinUserService.getWeixinUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信公众号用户基本信息.xls", "数据", WeixinUserRespVO.class,
                        BeanUtils.toBean(list, WeixinUserRespVO.class));
    }

}